<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.9 Equality Comparison Operators</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:29 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_08.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_10.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-9">4.9 Equality Comparison Operators</A></h2>

<P CLASS=para>
<A NAME="CH04.EQUAL1"></A><A NAME="CH04.EQUAL2"></A>The equality
comparison operators in Java are used for equal-to (<tt CLASS=literal>==</tt>)
and not-equal-to (<tt CLASS=literal>!=</tt>) comparison operations.
The equality comparison operators may appear in an equality expression:


<p>
<img align=middle src="./figs/jlr0419.gif" alt="[Graphic: Figure from the text]" width=450 height=81 border=0>

<P CLASS=para>
The
equality comparison operators are equal in precedence and are evaluated
from left to right. The <tt CLASS=literal>==</tt> and <tt CLASS=literal>!=</tt>
comparison operators can perform numerical comparisons, <tt CLASS=literal>boolean</tt>
comparisons, and reference type comparisons. Both of these operators
produce <tt CLASS=literal>boolean</tt> values.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_08.htm#JLR2-CH-4-SECT-8">Relational Comparison Operators</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-9.1">Equal-To Operator ==</A></h3>

<P CLASS=para>
<A NAME="CH04.EQUAL4"></A><A NAME="CH04.EQUAL5"></A>The equal-to operator <tt CLASS=literal>==</tt> performs a comparison
between its operands and returns a <tt CLASS=literal>boolean</tt> value.
It returns the pure value <tt CLASS=literal>true</tt> if the operands
are equal to each other; otherwise it returns the pure value <tt CLASS=literal>false</tt>.
The <tt CLASS=literal>==</tt> operator may appear as part of an equality
expression. The equal-to operator never throws an exception.

<P CLASS=para>
The operands of <tt CLASS=literal>==</tt> may be of any type,
but they must both be of the same kind of type or a compile-time
error occurs. If one operand is of an arithmetic type, the other
must also be of an arithmetic type. If one operand is of type <tt CLASS=literal>boolean</tt>,
the other must also be of type <tt CLASS=literal>boolean</tt>. If one
operand is a reference type, the other must also be a reference
type. Note that neither operand can be an expression that invokes
a <tt CLASS=literal>void</tt> method.

<P CLASS=para>
If both operands
are of arithmetic types, then the operator performs an arithmetic equality
comparison. The operator may perform type conversions on the operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>double</tt>, then the
other operand is converted to <tt CLASS=literal>double</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>float</tt>, the other
operand is converted to <tt CLASS=literal>float</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>long</tt>, the other
operand is converted to <tt CLASS=literal>long</tt>.

<P>
<li CLASS=listitem>Otherwise,
both operands are converted to <tt CLASS=literal>int</tt>.

<P>
</UL>
<P CLASS=para>
The
equality comparison of any two arithmetic values produces <tt CLASS=literal>true</tt>
if and only if both operands are the same value; otherwise the comparison
produces <tt CLASS=literal>false</tt>. The comparison of floating-point
data is governed by the following additional rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If
either operand is not-a-number (NaN), the comparison produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Positive infinity is a distinct value that is equal to itself,
and not equal to any other value.

<P>
<li CLASS=listitem>Negative infinity
is a distinct value that is equal to itself, and not equal to any
other value.

<P>
<li CLASS=listitem>Positive and negative zero are treated
as equal, so <tt CLASS=literal>-0.0==0.0</tt> produces <tt CLASS=literal>true</tt>.

<P>
</UL>
<P CLASS=para>
If both operands are <tt CLASS=literal>boolean</tt> values, the
operator performs a Boolean equality comparison. The comparison
produces <tt CLASS=literal>true</tt> if both operands are <tt CLASS=literal>true</tt>
or both operands are <tt CLASS=literal>false</tt>. Otherwise, the comparison
produces <tt CLASS=literal>false</tt>.

<P CLASS=para>
If both operands
are reference types, the operator performs an object equality comparison.
In order to perform this type of comparison, it must be possible
to cast the value of one of the operands to the type of the other
operand, or a compile-time error occurs. The comparison produces
<tt CLASS=literal>true</tt> if both of its operands refer to the same
object or if both of its operands are <tt CLASS=literal>null</tt>; otherwise
the comparison produces <tt CLASS=literal>false</tt>.

<P CLASS=para>
Because
the <tt CLASS=literal>==</tt> operator determines if two objects are
the same object, it is not appropriate for comparisons that need
to determine if two objects have the same contents. For example,
if you need to know whether two <tt CLASS=literal>String</tt> objects
contain the same sequences of characters, the <tt CLASS=literal>==</tt>
operator is inappropriate. You should use the <tt CLASS=literal>equals()</tt>
method instead:[4]

<blockquote class=footnote>
<P CLASS=para>[4] 
This is similar to the difference
in C between writing <tt CLASS=literal>string1==string2</tt> and
<tt CLASS=literal>strcmp(string1, string2)==0</tt>.
</blockquote>
<DIV CLASS=screen>
<P>
<PRE>
string1.equals (string2)  // Compares contents of strings
string1 == string2        // Compares actual string objects
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<A HREF="ch03_02.htm#JLR2-CH-3-SECT-2">Reference Types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-9.2">Not-Equal-To-Operator !=</A></h3>

<P CLASS=para>
The not-equal-to operator <tt CLASS=literal>!=</tt>
performs a comparison between its operands and returns
a <tt CLASS=literal>boolean</tt>
value. It returns the pure value <tt CLASS=literal>true</tt> if the
operands are not equal to each other; otherwise it returns the pure
value <tt CLASS=literal>false</tt>. The <tt CLASS=literal>!=</tt> operator
may appear as part of an equality expression. The not-equal-to operator
never throws an exception.

<P CLASS=para>
The operands of <tt CLASS=literal>!=</tt>
may be of any type, but they must both be of the same kind of type
or a compile-time error occurs. If one operand is of an arithmetic
type, the other must also be of an arithmetic type. If one operand
is of type <tt CLASS=literal>boolean</tt>, the other must also be of
type <tt CLASS=literal>boolean</tt>. If one operand is a reference type,
the other must also be a reference type. Note that neither operand
can be an expression that invokes a <tt CLASS=literal>void</tt> method.

<P CLASS=para>
If both operands are of arithmetic types, the operator performs
an arithmetic inequality comparison. The operator may perform type
conversions on the operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>double</tt>,
then the other operand is converted to
<tt CLASS=literal>double</tt>.

<P>
<li CLASS=listitem>Otherwise, if either operand
is of type <tt CLASS=literal>float</tt>, the other operand is converted
to <tt CLASS=literal>float</tt>.

<P>
<li CLASS=listitem>Otherwise, if either operand
is of type <tt CLASS=literal>long</tt>, the other operand is converted
to <tt CLASS=literal>long</tt>.

<P>
<li CLASS=listitem>Otherwise, both operands
are converted to <tt CLASS=literal>int</tt>.

<P>
</UL>
<P CLASS=para>
The inequality
comparison of any two arithmetic values produces <tt CLASS=literal>true</tt>
if and only if both operands are not the same value; otherwise the
comparison produces <tt CLASS=literal>false</tt>. The comparison of
floating-point data is governed by the following additional rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is not-a-number (NaN), the comparison produces
<tt CLASS=literal>true</tt>. NaN is the only value that compares as
not equal to itself.

<P>
<li CLASS=listitem>Positive infinity is a distinct
value that is equal to itself, and not equal to any other value.

<P>
<li CLASS=listitem>Negative infinity is a distinct value that is equal to itself,
and not equal to any other value.

<P>
<li CLASS=listitem>Positive and negative
zero are treated as equal, so <tt CLASS=literal>-0.0!=0.0</tt> produces
<tt CLASS=literal>false</tt>.

<P>
</UL>
<P CLASS=para>
If both operands are <tt CLASS=literal>boolean</tt>
values, the operator performs a Boolean inequality comparison. The
comparison produces <tt CLASS=literal>false</tt> if both operands are
<tt CLASS=literal>true</tt> or both operands are <tt CLASS=literal>false</tt>.
Otherwise, the comparison produces <tt CLASS=literal>true</tt>.

<P CLASS=para>
If both operands are reference types, the operator performs
an object equality comparison. In order to perform this type of
comparison, it must be possible to cast the value of one of the
operands to the type of the other operand, or a compile-time error
occurs. The comparison produces <tt CLASS=literal>true</tt> if both
of its operands refer to different objects and if both of its operands
are not <tt CLASS=literal>null</tt>; otherwise the comparison produces
<tt CLASS=literal>false</tt>.

<P CLASS=para>
Because the <tt CLASS=literal>!=</tt>
operator determines if two objects are different objects, it is
not appropriate for comparisons that need to determine if two objects
have different contents. For example, if you need to know whether
two <tt CLASS=literal>String</tt> objects contain different sequences
of characters, the <tt CLASS=literal>!=</tt> operator is inappropriate.
You should use the <tt CLASS=literal>equals()</tt>
method instead:[5]

<blockquote class=footnote>
<P CLASS=para>[5] 
This is similar to the difference in C
between writing <tt CLASS=literal>string1!=string2</tt>
and <tt CLASS=literal>strcmp(string1, string2)!=0</tt>.
</blockquote>
<DIV CLASS=screen>
<P>
<PRE>
!string1.equals (string2) // Compares contents of strings
string1 != string2        // Compares actual string objects
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<A HREF="ch03_02.htm#JLR2-CH-3-SECT-2">Reference Types</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_08.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_10.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Relational Comparison Operators</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Bitwise/Logical Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
